import os

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

#========== Models ==========
from models import Note
#========== Models ==========

#========== Main Page Handler ==========
class MainPage(webapp.RequestHandler):
	def get(self):
		template_values = {}
		path = os.path.join(os.path.dirname(__file__), 'index.html')
		self.response.out.write(template.render(path, template_values))
#========== Main Page Handler ==========

#========== Check Data Handler ==========
class Checkdata(webapp.RequestHandler):
	def post(self):
		path = os.path.join(os.path.dirname(__file__), 'checkdata.html')
      
		noter = db.GqlQuery("SELECT * FROM Note WHERE readkeyHash = :key", key = self.request.get('key'))
		
		template_values = {'notes': noter}
		self.response.out.write(template.render(path, template_values))
		
		#notew = db.GqlQuery("SELECT * FROM Note WHERE writekeyHash = :key", key = self.request.get('key'))
      
		#valuer = Note()
		#valuew = Note()
      
		#for note in noter:
			#valuer = note

		#for note in notew:
			#valuew = note
          
		#if valuer.readkeyHash == self.request.get('key'):
			#mode = "read"
			#template_values = {'note': valuer.dataCrypted, 'mode': mode, 'key': self.request.get('key')}
			#self.response.out.write(template.render(path, template_values))
		#else:
			#if valuew.writekeyHash == self.request.get('key'):
				#mode = "write"
				#template_values = {'note': valuew.dataCrypted, 'mode': mode, 'key': self.request.get('key')}
				#self.response.out.write(template.render(path, template_values))
			#else:
				#mode = "error"
				#template_values = {'error': "Key not found", 'mode': mode}
				#self.response.out.write(template.render(path, template_values))
				#return
#========== Check Data Handler ==========

#========== Add New Handler ==========
class AddNew(webapp.RequestHandler):
	def get(self):
		template_values = {}
		path = os.path.join(os.path.dirname(__file__), 'new.html')
		self.response.out.write(template.render(path, template_values))
#========== Add New Handler ==========

#========== New Data Handler ==========
class NewData(webapp.RequestHandler):
	def post(self):
		note = Note()
		note.readkeyHash = self.request.get('readkey')
		note.writekeyHash = self.request.get('writekey')
		note.dataCrypted = self.request.get('information')
		
		if(note.readkeyHash == note.writekeyHash):
			template_values = {}
			path = os.path.join(os.path.dirname(__file__), 'error.html')
			self.response.out.write(template.render(path, template_values))
			return
		
		note.put()
		
		template_values = {}
		path = os.path.join(os.path.dirname(__file__), 'done.html')
		self.response.out.write(template.render(path, template_values))
#========== New Data Handler ==========

#========== Admin Handler ==========
class Admin(webapp.RequestHandler):
	def get(self):
		notes = db.GqlQuery("SELECT * FROM Note")
		template_values = {'notes': notes}
		path = os.path.join(os.path.dirname(__file__), 'admin.html')
		self.response.out.write(template.render(path, template_values))
#========== Admin Handler ==========

#========== Update Handler ==========
class Update(webapp.RequestHandler):
	def post(self):
		template_values = {}
		path = os.path.join(os.path.dirname(__file__), 'update.html')
		self.response.out.write(template.render(path, template_values))
#========== Update Handler ==========

#========== Help Handler ==========
class Help(webapp.RequestHandler):
	def get(self):
		template_values = {}
		path = os.path.join(os.path.dirname(__file__), 'help.html')
		self.response.out.write(template.render(path, template_values))
#========== Help Handler ==========